package lumis.portal.principal.importprincipal;

import java.util.List;

import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.clock.ClockConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.service.GenericServiceClock;
import lumis.portal.service.ServiceException;
import lumis.portal.transaction.ITransaction;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;

/**
 * Clock service that executes the automatic run of the read/write principals
 * operation.
 * 
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class ImportPrincipalClock extends GenericServiceClock
{

	/**
	 * Class logger.
	 * 
	 * @since 6.0.0
	 */
	private static ILogger logger = LoggerFactory.getLogger(ImportPrincipalClock.class);

	/**
	 * Scheduled method that executes the automatic run of the read/write
	 * principals operation.
	 * 
	 * @since 6.0.0
	 */
	public void doTick(SessionConfig sessionConfig, ClockConfig clockConfig) throws ServiceException, PortalException
	{

		ITransaction transaction = PortalTransactionFactory.createTransaction();

		try
		{
			transaction.begin();

			exportPrincipal(transaction);

			importPrincipal(transaction);

			transaction.commit();

		}
		catch (Exception e)
		{
			logger.error(e);

		}
		finally
		{
			transaction.dispose();
		}

	}

	/**
	 * Method responsible for exporting the principals.
	 * 
	 * @param transaction
	 *            transaction used by the operation.
	 * 
	 * @since 6.0.0
	 */
	@SuppressWarnings("unchecked")
	private void importPrincipal(ITransaction transaction)
	{
		List<PrincipalWriterConfig> writers = ManagerFactory.getEntityManager().createQuery("from PrincipalWriterConfig where schedule=true").getResultList();

		for (PrincipalWriterConfig principalWriterConfig : writers)
		{

			try
			{
				ImportPrincipalManagerFactory.getImportPrincipalManager().writePrincipals(SessionConfig.getCurrentSessionConfig(), principalWriterConfig.getId(), transaction);
			}
			catch (Exception e)
			{
				// log only
				logger.error(e);
			}

		}
	}

	/**
	 * Method responsible for importing the principals.
	 * 
	 * @param transaction
	 *            transaction used by the operation.
	 * 
	 * @since 6.0.0
	 */
	@SuppressWarnings("unchecked")
	private void exportPrincipal(ITransaction transaction)
	{
		List<PrincipalReaderConfig> readers = ManagerFactory.getEntityManager().createQuery("from PrincipalReaderConfig where schedule=true").getResultList();

		for (PrincipalReaderConfig principalReaderConfig : readers)
		{
			try
			{
				ImportPrincipalManagerFactory.getImportPrincipalManager().readPrincipals(SessionConfig.getCurrentSessionConfig(), principalReaderConfig.getId(), transaction);
			}
			catch (Exception e)
			{
				// log only
				logger.error(e);
			}
		}
	}
}
